//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package testcase_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_testcase_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type TestResultInfoDbItem struct {
	TestResultId string `bson:"_id"`
	TestOk       bool   `bson:"testOk"`
	CaseId       string `bson:"caseId"`
	SpritId      string `bson:"spritId"`
	ProjectId    string `bson:"projectId"`
	CreateUserId string `bson:"createUserId"`
	CreateTime   int64  `bson:"createTime"`
	UpdateUserId string `bson:"updateUserId"`
	UpdateTime   int64  `bson:"updateTime"`
}

func (item *TestResultInfoDbItem) ToTestResultInfo() *project_testcase_api.TestResultInfo {
	return &project_testcase_api.TestResultInfo{
		TestResultId: item.TestResultId,
		TestOk:       item.TestOk,
		CaseId:       item.CaseId,
		SpritId:      item.SpritId,
		CreateUserId: item.CreateUserId,
		CreateTime:   item.CreateTime,
		UpdateUserId: item.UpdateUserId,
		UpdateTime:   item.UpdateTime,
	}
}

type _ResultDao struct {
	mc *mongo.Collection
}

func newResultDao(db *mongo.Database) (*_ResultDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_TEST_CASE + "result")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"projectId", 1}, {"caseId", 1}, {"spritId", 1}, {"createTime", 1}},
		},
		{
			Keys: bson.D{{"projectId", 1}, {"caseId", 1}, {"createTime", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_ResultDao{mc: mc}, nil
}

func (db *_ResultDao) Insert(ctx context.Context, dbItem *TestResultInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_ResultDao) Count(ctx context.Context, projectId, caseId string, filterBySpritId bool, spritId string) (uint32, error) {
	query := bson.M{"caseId": caseId, "projectId": projectId}
	if filterBySpritId {
		query["spritId"] = spritId
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_ResultDao) List(ctx context.Context, projectId, caseId string, filterBySpritId bool, spritId string, offset, limit int64) ([]*TestResultInfoDbItem, error) {
	query := bson.M{"caseId": caseId, "projectId": projectId}
	if filterBySpritId {
		query["spritId"] = spritId
	}

	cursor, err := db.mc.Find(ctx, query, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*TestResultInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_ResultDao) Exist(ctx context.Context, projectId, testResultId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId, "_id": testResultId})
	return count > 0, err
}

func (db *_ResultDao) Get(ctx context.Context, projectId, testResultId string) (*TestResultInfoDbItem, error) {
	retItem := &TestResultInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"projectId": projectId, "_id": testResultId}).Decode(retItem)
	return retItem, err
}

func (db *_ResultDao) Remove(ctx context.Context, projectId, testResultId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"projectId": projectId, "_id": testResultId})
	return err
}

func (db *_ResultDao) UpdateTestOk(ctx context.Context, projectId, testResultId string, testOk bool, updateUserId string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"projectId": projectId, "_id": testResultId},
		bson.M{"$set": bson.M{
			"testOk":       testOk,
			"updateUserId": updateUserId,
			"updateTime":   updateTime,
		}})
	return err
}

func (db *_ResultDao) DistinctIdByProject(ctx context.Context, projectId string) ([]string, error) {
	tmpList, err := db.mc.Distinct(ctx, "_id", bson.M{"projectId": projectId})
	if err != nil {
		return nil, err
	}
	retList := []string{}
	for _, tmpValue := range tmpList {
		if tmpValue.(string) != "" {
			retList = append(retList, tmpValue.(string))
		}
	}
	return retList, nil
}
